// @algorithm @lc id=287 lang=cpp 
// @title find-the-duplicate-number


// @test([1,3,4,2,2])=2
// @test([3,1,3,4,2])=3
// @test([1,1])=1
// @test([1,1,2])=1
class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int slow = 0, fast = 0;
        do {
            slow = nums[slow];
            fast = nums[ nums[fast] ];
        } while(slow != fast);
        slow = 0;
        while(slow != fast){
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
};